package com.baosight.sczx.zm.cf.service;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.zm.cf.domain.ZMCFContactList;

public class ServiceZMCF26 extends ServiceEPBase {

	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:26 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:44 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getBlock(EiConstant.queryBlock) == null){
			StaticTools.setQueryParameters(inInfo, "fdEndDate", StaticTools.getDate() + " 23:59:59", true);
		}
		else {
			String endTime = inInfo.getBlock(EiConstant.queryBlock).getCellStr(0, "fdEndDate");
			if(null != endTime && !"".equals(endTime)) {
				if(endTime.length() <= 10) {
					StaticTools.setQueryParameters(inInfo, "fdEndDate", endTime + " 23:59:59", true);
				}
			}
		}
		
		String fdSendDepartId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
		StaticTools.setQueryParameters(inInfo, "fdSendDepartId", fdSendDepartId, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCFContactList.query", new ZMCFContactList());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:59 张志芳
	 *    2011-3-18下午 15:52:30 张小飞  添加编号，名称的重复性校验
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdKeyId = TablePk.getInstance().getNextValue("T_CI_BUSINESSFILTER");//主键
		String fdSendDepartId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
		String fdSendPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//发送人ID
		//String fdSendPerson = UserSession.getLoginName();//发送人
		String fdSendTime = StaticTools.getDateTime();
		
		inInfo.getBlock("result").setCell(0, "fdKeyId", fdKeyId);
		inInfo.getBlock("result").setCell(0, "fdSendDepartId", fdSendDepartId);
		inInfo.getBlock("result").setCell(0, "fdSendPersonId", fdSendPersonId);
		inInfo.getBlock("result").setCell(0, "fdSendPerson", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdSendTime", fdSendTime);
		
		String title = "联系单：" + inInfo.getBlock("result").getCell(0, "fdTitle").toString();
		Documents.insertDoc(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), title, "日常工作", "其它", "");//添加附件		
				
		EiInfo outinfo = super.insert(inInfo, "ZMCFContactList.insert", "result");//主表新增			
		return outinfo;
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:14 张志芳
	 *    2011-3-18下午 15:52:30 张小飞  添加编号，名称的重复性校验
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdSendDepartId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
		String fdSendPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//发送人ID
		String fdSendTime = StaticTools.getDateTime();

		inInfo.getBlock("result").setCell(0, "fdSendDepartId", fdSendDepartId);
		inInfo.getBlock("result").setCell(0, "fdSendPersonId", fdSendPersonId);
		inInfo.getBlock("result").setCell(0, "fdSendPerson", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdSendTime", fdSendTime);
		
		String title = "联系单：" + inInfo.getBlock("result").getCell(0, "fdTitle").toString();
		Documents.insertDoc(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), title, "日常工作", "其他", "");//添加附件		
		
		
		EiInfo outInfo = super.update(inInfo, "ZMCFContactList.update");//主表更新
		return outInfo;
	}
	
	/**
	 * 发送联系单
	 * <p>Function: sendContact</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-7上午08:01:18 严洪浩
	 * </p>
	 *
	 */
	public EiInfo sendContact(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdSendTime", StaticTools.getDateTime());
		EiInfo _temp = super.update(inInfo, "ZMCFContactList.modifyFeedback");//从表更新
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:29 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//删除附件信息
		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
		{
			Documents.deleteDoc(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		
		EiInfo _temp = super.delete(inInfo, "ZMCFContactReceive.deleteByContact", "result");//删除从表
		_temp = super.delete(inInfo, "ZMCFContactList.delete", "result");//再删除主表数据
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30上午10:38:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}
